package starter.petstore;

/**
 * @ClassName PetApiActions
 * @Date 2024/11/8 21:17 37
 * @Author Jack Chen
 * @Description api-test-REST-Assured
 * @Version 1.0
 */

import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import io.restassured.http.ContentType;
import io.restassured.mapper.ObjectMapperType;
import net.serenitybdd.core.steps.UIInteractions;
import org.hamcrest.Matchers;

import static net.serenitybdd.rest.SerenityRest.*;

public class PetApiActions extends UIInteractions {

    private static Long id = 0L; //自己加的

    @Given("Kitty is available in the pet store")
    public Long givenKittyIsAvailableInPetStore() {
        Pet pet = new Pet("Kitty", "available");
        Long newId = given()
                .baseUri("https://petstore.swagger.io")
                .basePath("/v2/pet")
                .body(pet, ObjectMapperType.GSON)
                .accept(ContentType.JSON)
                //.log().all()
                .contentType(ContentType.JSON).post().getBody().as(Pet.class, ObjectMapperType.GSON).getId();
        id = newId; // 自己加的
        return newId;
    }

    @When("I ask for a pet using Kitty's ID: {long}")
    public void whenIAskForAPetWithId(Long id) {
        when().get("/" + PetApiActions.id);
    }

    @Then("I get Kitty as result")
    public void thenISeeKittyAsResult() {
        then().body("name", Matchers.equalTo("Kitty"));
    }

    @When("user find pets by {} status")
    public void userFindPetsBy(String status) {
        given()
                .baseUri("https://petstore.swagger.io")
                .basePath("/v2/pet")
                .accept(ContentType.JSON).
        when().get("/findByStatus?status=" + status).
                then().log().all().statusCode(200);
    }

    @Then("user can find pet {}")
    public void userCanFindPetPetName(long petId) {
        if (petId <= 2147483647) {
            then().body("id", Matchers.hasItem((int)petId));
        } else {
            then().body("id", Matchers.hasItems(petId));
        }

    }
}